package entity

import (
	"fmt"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//CswhRY 人员导入
type CswhRY struct {
	ID      int    `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	KHYF    string `gorm:"column:KHYF"`
	JYDW    string `gorm:"column:JYDW"`
	OrganID int    `gorm:"column:OrganID"`
	EMNO1   string `gorm:"column:EMNO1"`
	EMID1   int    `gorm:"column:EMID1"`
	ENNM    string `gorm:"column:ENNM"`
	// DSVL  float64 `gorm:"column:DSVL"`

	YGBZ string `gorm:"column:YGBZ"`
	YWTX string `gorm:"column:YWTX"`
}

//TableName 人员导入
func (CswhRY) TableName() string {
	return "T_JHCW_JXFP_YG"

}

//CswhRYEF 返回结构体
func CswhRYEF() base.EntityFunc {
	return func() interface{} {
		entity := CswhRY{}
		return &entity
	}
}

//CswhRYESF 返回结构体数组
func CswhRYESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []CswhRY
		return &entities
	}
}

//BeforeSave 保存前的触发器
func (i *CswhRY) BeforeSave(tx *gorm.DB) (err error) {
	sqlEx := "select dbo.GetBackDATE()"
	row := tx.Raw(sqlEx).Row()

	var emid1 int
	sqlEx = "select ID from T_SysUser where SysUserID=?"
	row = tx.Raw(sqlEx, i.EMNO1).Row()
	row.Scan(&emid1)
	if emid1 == 0 {
		return fmt.Errorf("员工号1:%s在系统中没有找到！", i.EMNO1)
	}

	var organid int
	sqlEx = "select ID  from T_Organization  where OrganName=?"
	row = tx.Raw(sqlEx, i.JYDW).Row()
	err = row.Scan(&organid)

	if organid == 0 {
		return fmt.Errorf("经营单位:%s在系统中没有找到！", i.JYDW)
	}

	return

}

func init() {
	base.DaoPoolObj.Put("CswhRY", base.NewDefaultDao(CswhRYEF(), CswhRYESF(), nil))

}
